<?php

class m120429_042150_create_chapter extends CDbMigration {

	public function safeUp() {
		$chapter = array(
			'id'=>'pk',
			'mangaId'=>'integer',
			'title'=>'string',
			'extension'=>'varchar(4)',
			'createdAt'=>'datetime',
			'updatedAt'=>'datetime',
		);
		$this->createTable('chapters', $chapter);
		$this->createIndex('index_chapter_manga', 'chapters', 'mangaId');
		$this->createIndex('index_chapter_title', 'chapters', 'title');
		$this->createIndex('index_chapter_updated_at', 'chapters', 'updatedAt');

		$auth = Yii::app()->authManager;

		$auth->createOperation('chapterCreate', 'Create Chapter');
		$auth->createOperation('chapterUpdate', 'Update Chapter');
		$auth->createOperation('chapterDelete', 'Delete Chapter');
		$auth->createOperation('chapterManage', 'Manage Chapter');

		$role = $auth->createRole('chapterModerator', 'Chapter Moderator');
		$role->addChild('chapterCreate');
		$role->addChild('chapterUpdate');
		$role->addChild('chapterDelete');
		$role->addChild('chapterManage');

		$auth->addItemChild('admin', 'chapterModerator');
	}

	public function safeDown() {
		$this->dropTable('chapters');

		$auth = Yii::app()->authManager;

		$auth->removeItemChild('admin', 'chapterModerator');

		$auth->removeItemChild('chapterModerator', 'chapterCreate');
		$auth->removeItemChild('chapterModerator', 'chapterUpdate');
		$auth->removeItemChild('chapterModerator', 'chapterDelete');
		$auth->removeItemChild('chapterModerator', 'chapterManage');

		$auth->removeAuthItem('chapterCreate');
		$auth->removeAuthItem('chapterUpdate');
		$auth->removeAuthItem('chapterDelete');
		$auth->removeAuthItem('chapterManage');

		$auth->removeAuthItem('chapterModerator');
	}

}